package edu.webtek.cloudresp;


import org.jdom2.Document;
import org.jdom2.Element;


public class RespSale extends RespBase {
	final private boolean ok;
	final private boolean itemSoldOut;
	final private boolean error;
	
	@Override
	public boolean hadError(){
		if(ok) return false;
		if(itemSoldOut || error) return true;
		return false;
	}
	
	@Override
	public String getErrorString(){
		if(itemSoldOut) return "Item has sold out";
		if(error) 		return "An error occured";		
		return super.getErrorString();
	}
	
	public RespSale(Document sales) {
		boolean ok = false, itemSoldOut = false, error = false;
		
		for(Element attr : sales.getRootElement().getChildren()){
			switch(attr.getName()){
			case "ok":
				ok = true;
				break;
			case "itemSoldOut":
				itemSoldOut = true;
				break;
			case "error":
				error = true;
				break;
			}
		}
		
		this.ok = ok;
		this.itemSoldOut = itemSoldOut;
		this.error = error;
	}
	@Override
	public String toString() {
		return "RespSales [getErrorString()=" + getErrorString() + "]";
	}
}
